package cn.edu.sdjzu.xg.eagle.service;

import cn.edu.sdjzu.xg.eagle.dao.TeacherDao;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherService implements TeacherServiceInterface{
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
    private static final UserServiceImpl USER_SERVICE_IMPL = UserServiceImpl.getInstance();
    private static final TeacherService teacherService = new TeacherService();
    private TeacherService() {}
    public static TeacherService getInstance() {
        return teacherService;
    }
    public boolean add(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            long teancherId = teacherDao.add(teacher,connection);
            teacher.setId(teancherId);
            User user = new User(teacher.getNo(),teacher.getNo(),teacher);
            added = USER_SERVICE_IMPL.add(user,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
            System.out.println(e.getMessage());
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }
    public boolean delete(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean deleted = false;
        try {
            long teacherId = teacherDao.delete(teacher,connection);
            teacher.setId(teacherId);
            User user = new User(teacher.getNo(),teacher.getNo(),teacher);
            deleted = USER_SERVICE_IMPL.delete(user,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
            System.out.println(e.getMessage());
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }
    public static void main(String[] args)throws SQLException{
        Teacher teacher = new Teacher("李昕熠","01",1l);
        /*TeacherService.getInstance().add(teacher);*/
        Teacher teacher1 = new Teacher("黄卓颖","02",2l);
        /*TeacherService.getInstance().add(teacher1);*/
        TeacherService.getInstance().delete(teacher);
        TeacherService.getInstance().delete(teacher1);
    }
    @Override
    public Collection<Teacher> findAll(List<Condition> conditionList) throws SQLException, BysjException {
        Collection<Teacher> teachers = TeacherDao.getInstance().findAll(conditionList);
        return teachers;
    }
    @Override
    public Collection<Teacher>findAll(List<Condition> conditionList, Pagination pagination) throws BysjException, SQLException{
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher>teachers = teacherDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return teachers;
    }

}
